package com.yixing.tech.system.controller;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.PageRow;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.service.CommonService;
import com.yixing.tech.system.service.IWebPageService;
import com.yixing.tech.common.vo.s3.WebPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yixing.tech.common.security.context.UserContext.getTenantId;

/**
 * @author yixing tech
 * at 2021/12/20
 * describe：
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/webpage")
public class WebPageController extends BaseControl {

    @Resource
    IWebPageService webPageService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/add")
    public Rs<Object> add(@RequestBody WebPageVO vo) throws BusinessException {
        commonService.fillCreateUser(vo);
        webPageService.add(vo);
        return Rs.ok();
    }

    @PostMapping("/list")
    public Rs<Object> list(@RequestBody WebPageVO vo) throws BusinessException {
        vo.setTenantId(getTenantId());
        List<WebPageVO> list = webPageService.list(vo);
        return Rs.ok(list);
    }

    @PostMapping("/update/{webPageId}")
    public Rs<Object> update(@RequestBody WebPageVO vo, @PathVariable String webPageId) throws BusinessException {
        commonService.fillUpdateUser(vo);
        vo.setId(webPageId);
        webPageService.update(vo);
        return Rs.ok();
    }

    @PostMapping("/listPage")
    public Rs<Object> list(@RequestBody PageRow<WebPageVO> pageRow) throws BusinessException {
        pageRow.getCondition(WebPageVO.class).setTenantId(getTenantId());
        return Rs.ok(webPageService.list(pageRow));
    }


    @GetMapping("/{webPageId}")
    public Rs<Object> get(@PathVariable String webPageId) throws BusinessException {
        return Rs.ok(webPageService.queryById(webPageId));
    }

    @DeleteMapping("/{webPageId}")
    public Rs<Object> delete(@PathVariable String webPageId) throws BusinessException {
        webPageService.deleteById(webPageId);
        return Rs.ok();
    }
}
